package com.fjzn.detect.core.result;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.fjzn.detect.core.utils.sign.RSAKeyBean;
import com.fjzn.detect.core.utils.sign.RSAUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 解密失败结果返回
 * @author yzh
 * @since 2021年04月25日16:31:44
 */
public class CommonRespUtils {

    public static void returnERR(ResponseResult result, HttpServletRequest request, HttpServletResponse response) throws Exception{
        String callback = request.getParameter("callback");
        if(StrUtil.isBlank(callback)){
            callback = request.getHeader("callback");
        }
        response.setContentType("application/json;charset=UTF-8");
        response.setHeader("Content-Type", "application/json;charset=UTF-8");
        if(StrUtil.isBlank(callback)){
            response.getWriter().append(JSON.toJSONString(result));
        }else{
            StringBuilder jsonp = new StringBuilder();
            jsonp.append(callback).append("(").append(JSON.toJSONString(result)).append(");");
            response.getWriter().append(jsonp.toString());
        }
    }

    public static byte[] decrypByRsaPri(byte[] data){
        return RSAUtils.decryptPriKey(data, RSAKeyBean.RSA_PRI);
    }

    public static String decrypByRsaPri(String data){
        return RSAUtils.decryptPriKey(data, RSAKeyBean.RSA_PRI);
    }
}
